package com.lyh.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyh.common.annotation.Log;
import com.lyh.common.core.controller.BaseController;
import com.lyh.common.core.domain.AjaxResult;
import com.lyh.common.core.domain.entity.SysUser;
import com.lyh.common.core.page.TableDataInfo;
import com.lyh.common.enums.BusinessType;
import com.lyh.common.enums.PurchaseStatusEnum;
import com.lyh.common.enums.TimeIntervalEnum;
import com.lyh.common.utils.SecurityUtils;
import com.lyh.common.utils.poi.ExcelUtil;
import com.lyh.system.domain.MemberPurchaseInfo;
import com.lyh.system.domain.req.MemberBatchPurchasAuditReq;
import com.lyh.system.domain.req.MemberPurchasAuditReq;
import com.lyh.system.domain.req.MemberPurchaseInfoPageReq;
import com.lyh.system.domain.resp.MemberPurchaseAllInfoResp;
import com.lyh.system.domain.resp.MemberPurchaseInfoResp;
import com.lyh.system.domain.vo.ReserveEvidenceVo;
import com.lyh.system.domain.vo.ReserveRecordVo;
import com.lyh.system.mapper.MemberPurchaseInfoMapper;
import com.lyh.system.service.IMemberPurchaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户预约报单信息Controller
 *
 * @author ruoyi
 * @date 2023-04-27
 */
@RestController
@RequestMapping("/app/info")
@Api(value = "预约单量", description = "预约单量")
public class MemberPurchaseInfoController extends BaseController {
    @Autowired
    private IMemberPurchaseInfoService memberPurchaseInfoService;
    @Autowired
    private MemberPurchaseInfoMapper memberPurchaseInfoMapper;

    /**
     * 查询用户预约报单信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        QueryWrapper<MemberPurchaseInfo> queryWrapper = new QueryWrapper<>();
        List<MemberPurchaseInfo> list = memberPurchaseInfoMapper.selectList(queryWrapper);
        return AjaxResult.success(list);
    }


    @ApiOperation("预约单量分页列表")
    @GetMapping("/webList")
    public TableDataInfo webList(MemberPurchaseInfoPageReq req) {
        startPage();
        List<MemberPurchaseInfoResp> list = memberPurchaseInfoService.selectMemberPurchaseUserInfoList(req);

        if (list != null && list.size() > 0) {
            list.stream().forEach(c -> {
                c.setStatusName(PurchaseStatusEnum.getEnum(c.getStatus()).getName());
                if (TimeIntervalEnum.getEnum(c.getTimeInterval()) != null) {
                    c.setTimeIntervalName(TimeIntervalEnum.getEnum(c.getTimeInterval()).getName());
                }
            });
        }
        return getDataTable(list);
    }

    @Log(title = "预约导出", businessType = BusinessType.EXPORT)
    @ApiOperation("预约导出")
    @PostMapping("/export")
    public void  export(HttpServletResponse response, @RequestBody MemberPurchaseInfoPageReq req)
    {
        req.setPageNum(null);
        req.setPageSize(null);
        List<MemberPurchaseInfoResp> list = memberPurchaseInfoService.selectMemberPurchaseUserInfoList(req);

        if (list != null && list.size() > 0) {
            list.stream().forEach(c -> {
                c.setStatusName(PurchaseStatusEnum.getEnum(c.getStatus()).getName());
                if (TimeIntervalEnum.getEnum(c.getTimeInterval()) != null) {
                    c.setTimeIntervalName(TimeIntervalEnum.getEnum(c.getTimeInterval()).getName());
                }
            });
        }
        ExcelUtil<MemberPurchaseInfoResp> util = new ExcelUtil<>(MemberPurchaseInfoResp.class);
        util.exportExcel(response, list, "预约明细导出");
    }


    @ApiOperation("预约单量列表汇总")
    @GetMapping("/webAllNum")
    public AjaxResult<MemberPurchaseAllInfoResp> webAllNum(MemberPurchaseInfoPageReq req) {
        return AjaxResult.success(memberPurchaseInfoService.webAllNum(req));
    }

    @ApiOperation("审核")
    @PostMapping("/audit")
    public AjaxResult audit(@Validated @RequestBody MemberPurchasAuditReq req) {
        return toAjax(memberPurchaseInfoService.audit(req));
    }

    @ApiOperation("批量审核")
    @PostMapping("/batchAudit")
    public AjaxResult batchAudit(@Validated @RequestBody MemberBatchPurchasAuditReq req) {
        return toAjax(memberPurchaseInfoService.batchAudit(req));
    }



//    /**
//     * 导出用户预约报单信息列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:info:export')")
//    @Log(title = "用户预约报单信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, MemberPurchaseInfo memberPurchaseInfo) {
//        List<MemberPurchaseInfo> list = memberPurchaseInfoService.selectMemberPurchaseInfoList(memberPurchaseInfo);
//        ExcelUtil<MemberPurchaseInfo> util = new ExcelUtil<MemberPurchaseInfo>(MemberPurchaseInfo.class);
//        util.exportExcel(response, list, "用户预约报单信息数据");
//    }

    /**
     * 获取用户预约报单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(memberPurchaseInfoService.selectMemberPurchaseInfoById(id));
    }

    /**
     * 新增用户预约报单信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "用户预约报单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberPurchaseInfo memberPurchaseInfo) {
        return toAjax(memberPurchaseInfoService.insertMemberPurchaseInfo(memberPurchaseInfo));
    }

    /**
     * 修改用户预约报单信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "用户预约报单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberPurchaseInfo memberPurchaseInfo) {
        return toAjax(memberPurchaseInfoService.updateMemberPurchaseInfo(memberPurchaseInfo));
    }

    /**
     * 删除用户预约报单信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "用户预约报单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(memberPurchaseInfoService.deleteMemberPurchaseInfoByIds(ids));
    }

    /*    *//**
     * 预约报单
     *//*
    @PostMapping("/reserve")
    @Log(title = "用户预约报单信息", businessType = BusinessType.UPDATE)
    public AjaxResult reserve(@RequestBody ReserveEvidenceVo rev) {
        String evidenceIds = rev.getEvidenceIds();
        String timeInterval = rev.getTimeInterval();
        if (isBlank(evidenceIds) || isBlank(timeInterval)) {
            throw new RuntimeException("必须选择资产通证，和周期");
        }
        memberPurchaseInfoService.reserve(evidenceIds, timeInterval);
        return AjaxResult.success();
    }*/

    /**
     * 预约报单
     */
    @PostMapping("/reserve")
    @Log(title = "用户预约报单信息", businessType = BusinessType.UPDATE)
    public AjaxResult reserve(@RequestBody ReserveEvidenceVo rev) {
        if (rev.getReserves() == null || rev.getReserves().size() == 0) {
            throw new RuntimeException("必须选择资产通证，和周期");
        }
        memberPurchaseInfoService.reserve(rev.getReserves());
        return AjaxResult.success();
    }

    @GetMapping("/reserveRecord")
    @Log(title = "用户预约报单信息", businessType = BusinessType.OTHER)
    public AjaxResult reserveRecord() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<ReserveRecordVo> list = memberPurchaseInfoService.getReserveRecord(user.getUserId());
        return AjaxResult.success(list);
    }
}
